﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.IO;

namespace Tesla.Content {
    /// <summary>
    /// Resource locator that locates file resources from the disk.
    /// </summary>
    public class FileResourceLocator : IResourceLocator {
        private String _rootDirectory;

        /// <summary>
        /// Gets the root directory the locator searches in.
        /// </summary>
        public string RootDirectory {
            get {
                return _rootDirectory;
            }
        }

        /// <summary>
        /// Creates a new file resource locator where
        /// the root directory is the title path.
        /// </summary>
        public FileResourceLocator() {
            _rootDirectory = ResourceUtils.TitlePath;
        }

        /// <summary>
        /// Creates a new file resource locator with the specified root directory.
        /// </summary>
        /// <param name="rootDir">Root directory, if not rooted then the relative directory path is combined with the title path.</param>
        public FileResourceLocator(String rootDir) {
            if(!Path.IsPathRooted(rootDir)) {
                _rootDirectory = Path.Combine(ResourceUtils.TitlePath, rootDir);
            } else {
                _rootDirectory = rootDir;
            }
        }

        /// <summary>
        /// Attempts to locate the specified file.
        /// </summary>
        /// <param name="fileName">File name of asset to locate</param>
        /// <returns>Located resource, if it exists, otherwise null</returns>
        /// <exception cref="System.NullReferenceException">Thrown if file name is null</exception>
        public IResource LocateResource(String fileName) {
            if(String.IsNullOrEmpty(fileName)) {
                throw new NullReferenceException(String.Format("File {0} string cannot be null", fileName));
            }

            if(!Path.IsPathRooted(fileName)) {
                fileName = Path.Combine(_rootDirectory, fileName);
            }

            if(File.Exists(fileName)) {
                return new FileResource(fileName);
            }
            return null;
        }

        /// <summary>
        /// Get the full qualified path to the asset with the specified file name.
        /// </summary>
        /// <param name="assetName">File name of the asset</param>
        /// <returns>Fully qualified path</returns>
        public String GetFullPath(String assetName) {
            return Path.GetFullPath(Path.Combine(_rootDirectory, assetName));
        }

        /// <summary>
        /// Gets the full path relative to.
        /// </summary>
        /// <param name="assetName">Name of the asset.</param>
        /// <param name="resource">The resource.</param>
        /// <returns></returns>
        public String GetFullPathRelativeTo(String assetName, IResource resource) {
            if(!(resource is FileResource) || !File.Exists(resource.FullName)) {
                return String.Empty;
            }

            return Path.Combine(Path.GetDirectoryName(resource.FullName), assetName);
        }
    }
}
